package cn.caipengbo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.caipengbo.bean.AppInfo;
import cn.caipengbo.helloandroid.R;

/**
 * Created by Myth on 2017/8/15.
 */

public class AppAdapter extends BaseAdapter{
    private List<AppInfo> appInfoList;
    private Context context;

    public AppAdapter(List<AppInfo> appInfoList, Context context) {
        this.appInfoList = appInfoList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return appInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return appInfoList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        // 简单优化，提升ListView的效率
        if (convertView == null) {
            // 注意获得的View 对象是从 Item布局文件中获取的
            view = View.inflate(context, R.layout.app_listview_item, null);
        } else {
            view = convertView;
        }
        // 获取每个控件
        ImageView imageView = (ImageView)view.findViewById(R.id.app_icon);
        TextView name = (TextView)view.findViewById(R.id.app_name);

        AppInfo appInfo = appInfoList.get(position);
        // 在item中填入值
        imageView.setImageDrawable(appInfo.getIcon());
        name.setText(appInfo.getAppName());

        return view;
    }
}
